package swarch.tests;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import swarch.messageflow.Assessment;
import swarch.messageflow.Mission;

public class SwarchTest {
	
	private static String IP_ADDRESS = "tcp://PhatChai-PC:61616";
	
	
	
	private static String QUEUE_MISSION_INPUT = "swarch.mission.input";
	private static String QUEUE_MISSION_OUTPUT = "swarch.mission.output";
	private static String QUEUE_ASSESSMENT_INPUT = "swarch.assessment.input";
	private static String QUEUE_ASSESSMENT_OUTPUT = "swarch.assessment.output";

	private static String MISSION_MESSAGE = "<message-type>Mission</message-type>";

	private Session session;
	private ActiveMQConnection connection;
	@Before
	public void initialize() throws Exception {
		connection = ActiveMQConnection
				.makeConnection(IP_ADDRESS);
		connection.start();
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	}

	@After
	public void after() throws JMSException {
		connection.close();
	}
	
	@Test
	public void testSendMissionChannel() throws Exception {
		Destination destination = session.createQueue(QUEUE_MISSION_INPUT);
		TextMessage simpleMessage = session.createTextMessage();
		simpleMessage.setText(MISSION_MESSAGE);

		System.out.println("---------------- MISSION ------------------");
		System.out.println("Send mission to queue: " + QUEUE_MISSION_INPUT);
		System.out.println("message: " + MISSION_MESSAGE);
		System.out.println("-------------------------------------------");

		MessageProducer producer = session.createProducer(destination);
		producer.send(simpleMessage);
		producer.send(simpleMessage);
	}

	@Test
	public void testReceiveMissionChannel() throws Exception {
		MessageConsumer consumer = session.createConsumer(session
				.createQueue(QUEUE_MISSION_OUTPUT));

		System.out.println("Receive message from queue: "
				+ QUEUE_MISSION_OUTPUT);
		ObjectMessage missionMessage = (ObjectMessage) consumer.receive(5000);
		ArrayList<Mission> missionList = (ArrayList<Mission>) missionMessage
				.getObject();

		System.out.println("Mission count: " + missionList.size());
		int i = 1;
		for (Mission m : missionList) {
			System.out.print((i++) + " Mission name: " + m.getMissionId()
					+ "\t");
			System.out.print("Staff name: " + m.getStaffId() + "\t");
			System.out.println("Assessment : " + m.getAssessmentId());
		}
		System.out.println("-------------------------------------------");
	}

	@Test
	public void testSendAssessmentChannel() throws Exception {
		ActiveMQConnection connection = ActiveMQConnection
				.makeConnection("tcp://localhost:61616");
		connection.start();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue(QUEUE_ASSESSMENT_INPUT);
		TextMessage simpleMessage = session.createTextMessage();

		// String assessId = "MIYAGI";
		String assessId = "FUGUSHIMA";
		// String assessId = "FOOBAR";

		String message = "<messages><message-type>Assessment</message-type><assessment-id>"
				+ assessId + "</assessment-id></messages>";
		simpleMessage.setText(message);

		MessageProducer producer = session.createProducer(destination);
		producer.send(simpleMessage);

		System.out.println("-------------- ASSESSMENT -----------------");
		System.out.println("Send mission to queue: " + QUEUE_ASSESSMENT_INPUT);
		System.out.println("message: " + message);
		System.out.println("-------------------------------------------");

	}

	@Test
	public void testReceiveAssessmentChannel() throws Exception {
		MessageConsumer consumer = session.createConsumer(session
				.createQueue(QUEUE_ASSESSMENT_OUTPUT));

		System.out.println("Receive message from queue: "
				+ QUEUE_ASSESSMENT_OUTPUT);

		ObjectMessage assessMessage = (ObjectMessage) consumer.receive(5000);
		Assessment assess = (Assessment) assessMessage.getObject();

		System.out.print("Receive message: ");
		if (assess == null) {
			System.out.println("Null");
		} else {
			System.out.println();
			System.out.println("Assessment:\t\t" + assess.getAssessId());
			System.out.println("Arrival Date:\t\t" + assess.getArrivalDate());
			System.out.println("Contact:\t\t" + assess.getEtcContact());
			System.out.println("GSM Status:\t\t" + assess.getGsmRemark());
			System.out.println("Internet Status:\t"
					+ assess.getInternetRemark());
			System.out.println("Landline Status:\t"
					+ assess.getLandlineRemark());
			System.out.println("Latitude:\t\t" + assess.getLatitude());
			System.out.println("Longtitude:\t\t" + assess.getLongtitude());
			System.out.println("Service offered:\t"
					+ assess.getServiceOffered());
		}
		System.out.println("-------------------------------------------");
	}
}
